package com.bookshopsystem.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bookshopsystem.dao.DaoFactory;
import com.bookshopsystem.mod.Book;
import com.bookshopsystem.mod.Category;
import com.bookshopsystem.mod.Publisher;

/**
 * Servlet implementation class Admin_BookService
 */
@WebServlet("/Admin_BookService")
public class Admin_BookService extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Admin_BookService() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String type=request.getParameter("type");
		String data;
		JSONObject jsonObject;
		switch (type) {
		case "bookCount":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			try {
				int count=DaoFactory.getBookDaoInstance().bookCount(
						jsonObject.getString("searchName"),
						jsonObject.getString("searchType"),
						Integer.parseInt(jsonObject.getString("pageCount")));
				if (count==0) {
					count=1;
				}
				response.getWriter().print("[{'pageNum':'"+count+"'}]");
			} catch (Exception e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
				response.getWriter().print("[{'pageNum':'1'}]");
			}
			break;
		case "search":
			try {
				data=request.getParameter("data");
				jsonObject=new JSONObject().fromObject(data);
				List<Book> books=DaoFactory.getBookDaoInstance().searchBookAdmin(
						jsonObject.getString("searchName"), 
						jsonObject.getString("searchType"),
						Integer.parseInt(jsonObject.getString("page")),
						Integer.parseInt(jsonObject.getString("pageCount")),
						jsonObject.getString("sort"),
						jsonObject.getString("sortType"));
				JSONArray jsonArray=new JSONArray().fromObject(books);
				response.getWriter().print(jsonArray);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		case "bookInfo":
			data=request.getParameter("data");
			
			try {
				List<Object> list=new ArrayList<Object>();
				List<Category> categories=DaoFactory.getCategoryDao().searchCategory("");
				list.add(categories);
				List<Publisher> publishers=DaoFactory.getPublisherDao().getPublisher(0);
				list.add(publishers);
				if (data!="0") {
					Book books=DaoFactory.getBookDaoInstance().getBookInfo(Integer.parseInt(data));
					list.add(books);
				}
				JSONArray jsonArray=new JSONArray().fromObject(list);
				response.getWriter().print(jsonArray);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			break;
		case "delete":
			data=request.getParameter("data");
			try {
				if (DaoFactory.getBookDaoInstance().deleteBookInfo(Integer.parseInt(data))) {
					response.getWriter().print("[{'deleteBook':'success'}]");
				}else {
					response.getWriter().print("[{'deleteBook':'false'}]");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.getWriter().print("[{'deleteBook':'false'}]");
			}
			break;
		case "update":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			Book book=(Book)JSONObject.toBean(jsonObject, Book.class);
			try {
				if (DaoFactory.getBookDaoInstance().updateBookInfo(book)) {
					response.getWriter().print("[{'update':'success'}]");
				}else {
					response.getWriter().print("[{'update':'false'}]");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.getWriter().print("[{'update':'false'}]");
			}
			break;
		case "add":
			data=request.getParameter("data");
			jsonObject=new JSONObject().fromObject(data);
			Book book_2=(Book)JSONObject.toBean(jsonObject, Book.class);
			try {
				if (DaoFactory.getBookDaoInstance().addBookInfo(book_2)) {
					response.getWriter().print("[{'update':'success'}]");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				response.getWriter().print("[{'update':'false'}]");
			}
			break;
		default:
			break;
		}
	}

}
